package andev.axplorer.FileWatcher;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

import andev.aandroidlib.activity.AActivity;
import andev.axplorer.R;

/**
 * Created by lzy1g on 2015/8/3.
 */
public class VideoFileWatcher extends AActivity implements View.OnClickListener {
    private static boolean bMax = false;
    private static int potision = 0;
    private ImageButton ib_close, ib_max;
    private VideoView vv;
    private MediaController mediaController;

    private LinearLayout watcher_window, vvContainer;
    private RelativeLayout titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (!bMax) {
            setTheme(android.R.style.Theme_Holo_Dialog_NoActionBar);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            setTheme(R.style.max_filewatcher_theme);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_filewatcher);
        ib_close = (ImageButton) findViewById(R.id.ib_close);
        ib_close.setOnClickListener(this);
        ib_max = (ImageButton) findViewById(R.id.ib_max);
        ib_max.setOnClickListener(this);
        vv = (VideoView) findViewById(R.id.vv);
        vvContainer = (LinearLayout) findViewById(R.id.vvContainer);
        watcher_window = (LinearLayout) findViewById(R.id.watcher_window);
        if (!bMax) {
            getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, getScreenSize().x);
            ib_max.setImageResource(R.mipmap.ic_window_max);
        } else {
            ib_max.setImageResource(R.mipmap.ic_window_normal);
        }

		/*防止外部点击关闭*/
        setFinishOnTouchOutside(false);
        InitComp();
    }

    private void InitComp() {
        mediaController = new MediaController(this);
        File file = null;
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            file = (File) bundle.getSerializable("file");
        } else {
            Uri uri = intent.getData();
            try {
                file = new File(new URI(uri.toString()));
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
        vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaController.show();
            }
        });

        vv.setVideoPath(file.getAbsolutePath());
        vv.setMediaController(mediaController);
        vv.seekTo(potision);
        vv.start();

    }

    @Override
    public void onBackPressed() {
        doublePressExit(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_close:
                finish();
                break;
            case R.id.ib_max:
                potision = vv.getCurrentPosition();
                //recreate();
                //when we change the orientation of activity,it will automatically recreate
                if (bMax)
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                else
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                bMax = !bMax;
                break;
        }
    }
}
